package com.sskj.base.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sskj.base.R;

import java.util.List;

public abstract class BaseAdapter<T> extends BaseQuickAdapter<T, ViewHolder> {
    private boolean setEmpty;

    public BaseAdapter(int layoutResId, @Nullable List<T> data, RecyclerView recyclerView) {
        super(layoutResId, data);
        if (recyclerView != null) {
            this.bindToRecyclerView(recyclerView);
        }
    }

    public BaseAdapter(int layoutResId, @Nullable List<T> data, RecyclerView recyclerView, boolean addEmptyView) {
        super(layoutResId, data);
        this.bindToRecyclerView(recyclerView);
        setEmpty = addEmptyView;
    }

    @Override
    protected void convert(ViewHolder helper, T item) {
        bind(helper, item);
    }

    public abstract void bind(ViewHolder holder, T item);


    @Override
    public void setNewData(@Nullable List<T> data) {
        if (data == null || data.size() == 0) {
            if (setEmpty) {
                setEmtyView();
                setEmptyText("暂无数据");
            }
        }
        super.setNewData(data);
    }

    public void setEmtyView() {
        this.setEmptyView(R.layout.empty_view);
    }


    public void setEmptyText(String text) {
        TextView textView = getEmptyView().findViewById(R.id.empty_text);
        textView.setText(text);
    }
}
